package com.cbh.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cbh.api.model.ApiResult;
import com.cbh.domain.service.InsuranceCompanyService;
import com.cbh.entity.InsuranceCompany;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * ClassName: InsuranceCompanyController<br>
 * Description: 保险公司管理.<br>
 * Reason: TODO ADD REASON(可选) <br>
 *
 * @author Sean
 * @date: 2017年3月8日 下午7:29:54
 * @version V1.0
 * @since JDK 1.8
 */
@Api(description = "保险公司管理")
@RestController
@RequestMapping("/sign/v3/insuranceCompanys")
public class InsuranceCompanyController {
	@Autowired
	InsuranceCompanyService icService;

	/**
	 * 新增保险公司
	 * 
	 * @param icId
	 * @param icCode
	 * @param icName
	 * @param icDetail
	 * @param icLogo
	 * @return
	 */
	@ApiOperation(value = "保险公司信息保存", httpMethod = "POST")
	@RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json")
	public ApiResult<String> save(
			@ApiParam(value = "提交数据", required = true) @RequestBody(required = true) InsuranceCompany ob) {
		ApiResult<String> apiResult = new ApiResult<>();
		try {
			String re = icService.save(ob);
			return apiResult.success(re);
		} catch (Exception e) {
			return apiResult.fail(e.getMessage());
		}
	}

	/**
	 * 根据Code获取保险公司信息
	 * 
	 * @param icCode
	 * @return
	 */
	@ApiOperation(value = "根据Code获取保险公司信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", name = "code", dataType = "String", required = true) })
	@RequestMapping(value = "/code/{code}", method = RequestMethod.GET, produces = "application/json")
	public ApiResult<InsuranceCompany> getByIcCode(@PathVariable("code") String code) {
		ApiResult<InsuranceCompany> apiResult = new ApiResult<>();
		try {
			InsuranceCompany icByCode = icService.getIcByCode(code);
			return apiResult.success(icByCode);
		} catch (Exception e) {
			return apiResult.fail(e.getMessage());
		}
	}

	@ApiOperation(value = "根据ID获取保险公司信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true) })
	@RequestMapping(value = "/id/{id}", method = RequestMethod.GET, produces = "application/json")
	public ApiResult<InsuranceCompany> getByIcId(@PathVariable("id") String id) {
		ApiResult<InsuranceCompany> apiResult = new ApiResult<>();
		try {
			InsuranceCompany one = icService.getOne(id);
			return apiResult.success(one);
		} catch (Exception e) {
			return apiResult.fail(e.getMessage());
		}
	}

	/**
	 * 根据ID删除保险公司信息
	 * 
	 * @param icId
	 * @return
	 */
	@ApiOperation(value = "根据ID删除保险公司信息", httpMethod = "DELETE", nickname = "根据ID删除保险公司信息")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true) })
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json")
	public ApiResult<Boolean> deleteByIcId(@PathVariable("id") String id) {
		ApiResult<Boolean> apiResult = new ApiResult<>();
		try {
			Boolean re = icService.delete(id);
			return apiResult.success(re);
		} catch (Exception e) {
			return apiResult.fail(e.getMessage());
		}
	}

	/**
	 * 获取全部保险公司信息
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取全部保险公司信息", httpMethod = "GET")
	@RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json")
	public ApiResult<List<InsuranceCompany>> getAll() {
		ApiResult<List<InsuranceCompany>> apiResult = new ApiResult<>();
		try {
			List<InsuranceCompany> obs = icService.companies();
			return apiResult.success(obs);
		} catch (Exception e) {
			return apiResult.fail(e.getMessage());
		}
	}

}
